﻿using PelitaProduksi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PelitaProduksi.View.Panel
{
    public partial class SearchRajut : Form
    {
        Main main; int code;

        public SearchRajut(Main main, int code)
        {
            InitializeComponent();
            this.main = main;
            this.code = code;
        }
        ~SearchRajut()
        {
            this.main.LIST_FORMS.Remove(this.code);
            this.main.BringToFront();
            this.main.Activate();
        }

        private void showList()
        {
            string tgl1, tgl2;
            tgl1 = string.Format("{0:yyyy}-{0:MM}-{0:dd}", date1.Value);
            tgl2 = string.Format("{0:yyyy}-{0:MM}-{0:dd}", date2.Value);
            List<Rajut> list = Rajut.getRajut(tgl1, tgl2);
            if (list == null) return;
            foreach(Rajut r in list)
            {
                data.Rows.Add(r.NoReg, r.Tanggal, r.Nama1, r.Nama2, r.Nama3, r.Keterangan, r.UpdateDate, r.UpdateTime, r.User);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int count = data.SelectedCells.Count;
            if(count<1)
            {
                MessageBox.Show("Pilih rajut dahulu");
                return;
            }
            int index = data.SelectedCells[0].RowIndex;
            object noRajut = data.Rows[index].Cells["noReg"].Value;
            if (noRajut == null)
            {
                MessageBox.Show("Nomor rajut kosong");
                return;
            }
            else
            {
                // Open Form Rajut
                FormRajut rajut = new FormRajut(main, main.CODE, noRajut.ToString());
                main.LIST_FORMS.Add(main.CODE++, rajut);
                rajut.Activate();
                rajut.BringToFront();
                rajut.Visible = true;
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            showList();
        }
    }
}
